package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.IScenicCommentCacheService;
import cn.wolfcode.luowowo.cache.vo.ScenicCommentStats;
import cn.wolfcode.luowowo.common.query.ScenicDetailQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.scenic.domain.Photo;
import cn.wolfcode.luowowo.scenic.domain.ScenicComment;
import cn.wolfcode.luowowo.scenic.domain.ScenicCommentReply;
import cn.wolfcode.luowowo.scenic.domain.ScenicDetail;
import cn.wolfcode.luowowo.scenic.service.*;
import cn.wolfcode.luowowo.search.service.IScenicCommentSearchService;
import cn.wolfcode.luowowo.search.template.ScenicCommentTemplate;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/scenic")
public class ScenicController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicDetailService scenicDetailService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicCommentReplyService commentReplyService;
    @Reference
    private IScenicCommentSearchService scenicSearchService;
    @Reference
    private IScenicThemeService themeService;
    @Reference
    private IPhotoService photoService;
    @Reference
    private IScenicCommentCacheService scenicCommentCacheService;



    //@RequiredLogin
    @ResponseBody
    @RequestMapping("isThumbup")
    public Object isThumbup(Long commentId, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        boolean flag = scenicCommentCacheService.isThumbup(commentId,user.getId());
        result.setSuccess(flag);
        //result.setData();
        return result;
    }




    //@RequiredLogin
    @ResponseBody
    @RequestMapping("/thumbup")
    public AjaxResult thumbup(Long commentId, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        result= scenicCommentCacheService.changeThumbup(commentId,user.getId());
        ScenicCommentStats commentStats = scenicCommentCacheService.getCommentStats(commentId);
        return result.addData(commentStats);
    }

    @RequestMapping("/searchPage")
    public String searchPage(@ModelAttribute("qo") ScenicDetailQueryObject qo, Model model) {
        PageInfo<ScenicDetail> pageInfo = scenicDetailService.listByThemeId(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "scenic/searchPageTpl";
    }


    @RequestMapping("/detail")
    public String getScenicDetail(Long id, Model model, @LoginUser UserInfo user) {
        //scenicDetail景点信息
        ScenicDetail scenicDetail = scenicDetailService.get(id);
        model.addAttribute("entity", scenicDetail);
        //toasts
        List<Destination> toasts = destinationService.listToasts(scenicDetail.getDestId());
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        //点评信息,如果用户登录了,根据用户ID查询是否已经评价
//        if (user != null) {
//            //isThumbup
//            boolean isThumbup =scenicCommentCacheService.isThumbup()
//            model.addAttribute("isThumbup", isThumbup);
//        }


        return "scenic/detail";
    }


    @RequestMapping("/list")
    public String listScenicDetail(@ModelAttribute("qo") ScenicDetailQueryObject qo, Model model) {
        //不带目的地访问跳转回主页
        if (qo.getDestId() == null) {

        }
        //toasts
        List<Destination> toasts = destinationService.listToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        //scenic (dest)
        ScenicDetail scenicDetail = scenicDetailService.getByDestId(qo.getDestId());
        model.addAttribute("scenic", scenicDetail);
        //scenicDetails
        List<ScenicDetail> scenicDetails = scenicDetailService.listTop5ByDestId(qo.getDestId());
        model.addAttribute("scenicDetails", scenicDetails);
        //热门景点 hotScenics
        List<ScenicDetail> hotScenics = scenicDetailService.listHotScenicsByDestId(qo.getDestId());
        model.addAttribute("hotScenics", hotScenics);
        //地点主题
        model.addAttribute("themes", themeService.listByDestId(qo.getDestId()));
        return "scenic/list";
    }

    // @RequiredLogin
    @ResponseBody
    @RequestMapping("/addScenicComment")
    public AjaxResult addScenicComment(ScenicComment scenicComment, @LoginUser UserInfo user) {

        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }
        //设置用户信息
        scenicComment.setUser(user);

        scenicCommentService.saveOrUpdate(scenicComment);
        return result;
    }

    /**
     * 获取点评列表
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/commentList")
    public String commentList(@ModelAttribute("qo") ScenicDetailQueryObject qo, Model model) {
        String forward = "";
        //使用mySQl
        if (qo.getType() == 1) {
            //查询全部
            qo.setOrderBy("thumbupnum desc,createTime desc");
            model.addAttribute("pageInfo", scenicCommentService.query(qo));


            forward = "scenic/commentListTpl";
        } else if (qo.getType() == 2) {
            //使用es查询
            Page<ScenicCommentTemplate> page = scenicSearchService.query(qo);
            model.addAttribute("page", page);
            forward = "scenic/commentListTplByES";
        }

        return forward;
    }

    /**
     * 添加点评评论
     *
     * @param scenicCommentReply
     * @return
     */
    // @RequiredLogin
    @ResponseBody
    @RequestMapping("/addReplyComment")
    public AjaxResult addReplyComment(ScenicCommentReply scenicCommentReply, @LoginUser UserInfo user) {
        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);
            return result;
        }

        //设置用户信息
        try {
            scenicCommentReply.setUser(user);
            //设置创建时间
            scenicCommentReply.setCreateTime(new Date());
            commentReplyService.saveOrUpdate(scenicCommentReply);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("评论失败");
        }
        return result;
    }


    @Value("${file.dir}")
    private String dir;


    //图片上传  有小bug待解决,多次在同一个框上传会上传多张图片
    @ResponseBody
    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file, Photo photo) {
        AjaxResult result = new AjaxResult();
        String fileName = UploadUtil.upload(file, dir);
        fileName = "/" + fileName;
        photo.setUrl(fileName);
        photoService.save(photo);
        result.addData(fileName);
        return result;
    }


}
